(function () {



    class scanQuestion{


        constructor(question){
            this.count=0;
            this.maxCount=70;
            this.interVal=300;
            this.question=question;
        }


        async scanDom() {

            try{
                let question=document.querySelector("div.fight-title p");
                let answers=document.querySelectorAll("div.fight-answers a");

                if(question&&answers&&question.innerText===this.question.title){
                    answers=Array.from(answers);

                    if(this.question.index>answers.length){
                        SendMessage(`${question.id}疑似错误,选项超过最大值`);
                    }

                    if(!(answers.some(a=>a.classList.contains("answers-right")||a.classList.contains("answers-right")))){

                        let scanClickIndex=0;

                        do{

                            if(this.question.index===0||this.question.index>answers.length){
                                answers[Math.ceil(Math.random()*answers.length)].click();
                            }else {
                                answers[this.question.index-1].click();
                            }
                            await sleep(this.interVal);
                            scanClickIndex++;
                        }while (scanClickIndex<10&&!(await this.scanCheckIsClick(0)));

                        await sleep(this.interVal);
                        this.count=this.maxCount+1;
                        this.check();
                    }

                }else {
                    await sleep(this.interVal);
                    this.count++;
                }
            }catch (ex){
                await sleep(this.interVal);
                this.count++;
            }

            if(this.count<this.maxCount) this.scanDom();
        }



        async scanCheckIsClick(index=0) {

            try{
                let question=document.querySelector("div.fight-title p");
                let answers=document.querySelectorAll("div.fight-answers a");

                if(question&&answers){
                    if(question.innerText!==this.question.title){
                        return true;
                    }else {
                        answers=Array.from(answers);
                        return answers.some(a=>a.classList.contains("answers-right")||a.classList.contains("answers-wrong"));
                    }
                }else {
                    await sleep(this.interVal);
                    return index>2||(await this.scanCheckIsClick(index++));
                }

            }catch (ex){
                await sleep(this.interVal);
                return index>2||(await this.scanCheckIsClick(index++));
            }
        }


        async check(index=0) {

            try{
                let question=document.querySelector("div.fight-title p");
                let answers=document.querySelectorAll("div.fight-answers a");

                if(question&&answers&&question.innerText===this.question.title) {
                    answers = Array.from(answers);

                    let wrongIndex=0;
                    let rightIndex=0;
                    let rightStr="";

                    answers.forEach((a,i)=>{
                        if(a.classList.contains("answers-wrong")){
                            wrongIndex=i+1;
                        }

                        if(a.classList.contains("answers-right")){
                            rightIndex=i+1;
                            rightStr=a.innerText;
                        }
                    });

                    if(wrongIndex!==0&&!(this.question.index===0||this.question.index>answers.length)){
                        SendMessage(`${question.id}疑似错误,选项貌似为第${rightIndex}个,内容为:${rightStr}`);

                        SendMessage({
                            title:this.question.title
                            ,text:rightStr
                            ,rightIndex:rightIndex
                            ,dataID:this.question.dataID
                            ,id:this.question.id
                        })

                    }

                }

            }catch (ex){
                await sleep(this.interVal);
                if(index===0) this.check(1);
            }

        }




    }


    let webSocket = new WebSocket("ws://localhost:8010/");



    async function sleep(ms) {
        return new Promise((s,r)=>{
            setTimeout(()=>(s()),ms);
        });
    }


    webSocket.onopen = function(){
        //document.body.innerText="open";
        //scanDom();
    };

    webSocket.onclose = function(){
        //document.body.innerText="close";
    };

    webSocket.onmessage = function(e){

        try{

            let question=JSON.parse(e.data);
           // SendMessage(e);
            new scanQuestion(question).scanDom();
        }catch (e) {
            //SendMessage("数据解析错误"+e.toString());
        }

        //console.log(e.data);
        //document.body.innerText=e.data;
    };


    window.addEventListener("onunload",()=>{

        if(webSocket){
            webSocket.close();
        }


    });


    function SendMessage(s) {

        if(typeof(s)==="string"||typeof (s)==="number"){
            webSocket.send(JSON.stringify({state:"errStr",info:s}));
        }else {
            s.state="errObj";
            webSocket.send(JSON.stringify(s));
        }



    }

    /*async function scanDom() {

        try{
            let question=document.querySelector("div.fight-title p");
            let answers=document.querySelectorAll("div.fight-answers a");
            let continueFight=document.querySelector("div.result-btns.result-fadein a");


            let data=null;

            if(question&&answers){
                answers=Array.from(answers);
                data={question:question.innerText||"",answers:answers.map(a=>a.innerText).join(",")||""};
                SendMessage(JSON.stringify(data));


                if(!(answers.some(a=>a.classList.contains("answers-right")||a.classList.contains("answers-right")))){
                    answers[Math.ceil(Math.random()*answers.length)].click();
                    await sleep(2000);
                }

            }else if(continueFight&&continueFight.innerText==="继续挑战"){
                continueFight.click();
                await sleep(5000);
            }else {
                SendMessage("没有提取到信息");
                await sleep(2000);
            }
        }catch (ex){
            SendMessage(JSON.stringify({error:ex}));
            await sleep(2000);
        }

        await sleep(1000);
        scanDom();
    }*/


    async function fightDom() {

        try{
            let continueFight=document.querySelector("div.result-btns.result-fadein a");

           if(continueFight&&continueFight.innerText==="继续挑战"){
                continueFight.click();
                await sleep(5000);
            }else {
                await sleep(1000);
            }
        }catch (ex){
            await sleep(1000);
        }

        fightDom();
    }

    fightDom();

})();